using System;
using System.Collections.Generic;

public class TK_ChessBoard : BaseFunctionalChessBoard
{
    private Dictionary<int, (Pos, (Pos, Pos))> campForwardOrientationDict = new Dictionary<int, (Pos, (Pos, Pos))>()
    {
        [0] = (new Pos(1, 0), (new Pos(0, -1), new Pos(0, 1))),
        [1] = (new Pos(-1, 0), (new Pos(0, -1), new Pos(0, 1))),
        [2] = (new Pos(0, 1), (new Pos(-1, 0), new Pos(1, 0)))
    };
    public TK_ChessBoard() : this(false)
    {

    }
    protected override BaseFunctionalChessBoard GetUninitializedInstance()
    {
        return new TK_ChessBoard(true);
    }
    private TK_ChessBoard(bool skipDefaultInit)
    {
        grids = new StandardGrid[19, 19];
        for (int i = 0; i < grids.GetLength(0); i++)
        {
            for (int j = 0; j < grids.GetLength(1); j++)
            {
                if ((i >= 5 && i <= 13) || (j >= 5 && j <= 13))
                {
                    Pos pos = new Pos(i, j);
                    int camp;
                    if ((i >= 5 && i <= 13) && j < 5)
                    {
                        camp = 2;
                    }
                    else if ((i >= 5 && i <= 13) && j > 13)
                    {
                        camp = 3;
                    }
                    else if ((j >= 5 && j <= 13) && i < 5)
                    {
                        camp = 0;
                    }
                    else if ((j >= 5 && j <= 13) && i > 13)
                    {
                        camp = 1;
                    }
                    else
                    {
                        camp = -1;
                    }
                    bool isPalace = ((i >= 8 && i <= 10 && (j <= 2 || j >= 16)) || (j >= 8 && j <= 10 && (i <= 2 || i >= 16))) ? true : false;
                    grids[i, j] = new TKGrid(pos, camp, isPalace);
                }
            }
        }
        List<int> campTeamList = new List<int>() { 0, 0, 1 };
        List<(Pos, Type, int)> chessTypeCampList = new List<(Pos, Type, int)>()
        {
            (new Pos(3, 5),typeof(TKChessBing),0),
            (new Pos(3, 7),typeof(TKChessBing),0),
            (new Pos(3, 9),typeof(TKChessBing),0),
            (new Pos(3, 11),typeof(TKChessBing),0),
            (new Pos(3, 13),typeof(TKChessBing),0),
            (new Pos(2, 6),typeof(TKChessPao),0),
            (new Pos(2, 12),typeof(TKChessPao),0),
            (new Pos(0, 5),typeof(TKChessChe),0),
            (new Pos(0, 13),typeof(TKChessChe),0),
            (new Pos(0, 6),typeof(TKChessMa),0),
            (new Pos(0, 12),typeof(TKChessMa),0),
            (new Pos(0, 7),typeof(TKChessXiang),0),
            (new Pos(0, 11),typeof(TKChessXiang),0),
            (new Pos(0, 8),typeof(TKChessShi),0),
            (new Pos(0, 10),typeof(TKChessShi),0),
            (new Pos(0, 9),typeof(TKChessShuai),0),

            (new Pos(15, 5),typeof(TKChessBing),1),
            (new Pos(15, 7),typeof(TKChessBing),1),
            (new Pos(15, 9),typeof(TKChessBing),1),
            (new Pos(15, 11),typeof(TKChessBing),1),
            (new Pos(15, 13),typeof(TKChessBing),1),
            (new Pos(16, 6),typeof(TKChessPao),1),
            (new Pos(16, 12),typeof(TKChessPao),1),
            (new Pos(18, 5),typeof(TKChessChe),1),
            (new Pos(18, 13),typeof(TKChessChe),1),
            (new Pos(18, 6),typeof(TKChessMa),1),
            (new Pos(18, 12),typeof(TKChessMa),1),
            (new Pos(18, 7),typeof(TKChessXiang),1),
            (new Pos(18, 11),typeof(TKChessXiang),1),
            (new Pos(18, 8),typeof(TKChessShi),1),
            (new Pos(18, 10),typeof(TKChessShi),1),
            (new Pos(18, 9),typeof(TKChessShuai),1),

            (new Pos(5, 3),typeof(TKChessBing),2),
            (new Pos(7, 3),typeof(TKChessBing),2),
            (new Pos(9, 3),typeof(TKChessBing),2),
            (new Pos(11, 3),typeof(TKChessBing),2),
            (new Pos(13, 3),typeof(TKChessBing),2),
            (new Pos(6, 2),typeof(TKChessPao),2),
            (new Pos(12, 2),typeof(TKChessPao),2),
            (new Pos(5, 0),typeof(TKChessChe),2),
            (new Pos(13, 0),typeof(TKChessChe),2),
            (new Pos(6, 0),typeof(TKChessMa),2),
            (new Pos(12, 0),typeof(TKChessMa),2),
            (new Pos(7, 0),typeof(TKChessXiang),2),
            (new Pos(11, 0),typeof(TKChessXiang),2),
            (new Pos(8, 0),typeof(TKChessShi),2),
            (new Pos(10, 0),typeof(TKChessShi),2),
            (new Pos(9, 0),typeof(TKChessShuai),2),
            (new Pos(5, 1),typeof(TKChessXie),2),
            (new Pos(13, 1),typeof(TKChessXie),2),
            (new Pos(8, 2),typeof(TKChessDan),2),
            (new Pos(10, 2),typeof(TKChessDan),2),
            (new Pos(7, 2),typeof(TKChessGou),2),
            (new Pos(11, 2),typeof(TKChessGou),2),
            (new Pos(5, 4),typeof(TKChessDun),2),
            (new Pos(7, 4),typeof(TKChessDun),2),
            (new Pos(9, 4),typeof(TKChessDun),2),
            (new Pos(11, 4),typeof(TKChessDun),2),
            (new Pos(13, 4),typeof(TKChessDun),2),
        };
        fieldInfo = new FieldInfo(campTeamList, chessTypeCampList, this);
        if (!skipDefaultInit)
        {
            fieldInfo.Init(chessTypeCampList, this);
        }
    }
    public Pos GetForwardDirectionByCamp(int camp)
    {
        if (!campForwardOrientationDict.ContainsKey(camp))
        {
            throw new Exception();
        }
        return campForwardOrientationDict[camp].Item1;
    }
    public Pos GetOrientation1DirectionByCamp(int camp)
    {
        if (!campForwardOrientationDict.ContainsKey(camp))
        {
            throw new Exception();
        }
        return campForwardOrientationDict[camp].Item2.Item1;
    }
    public Pos GetOrientation2DirectionByCamp(int camp)
    {
        if (!campForwardOrientationDict.ContainsKey(camp))
        {
            throw new Exception();
        }
        return campForwardOrientationDict[camp].Item2.Item2;
    }
}
